package com.nidu.demo.dict.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.dict.convertor.DictTypeConvertor;
import com.nidu.demo.dict.dto.DictTypeCO;
import com.nidu.demo.dict.gateway.DictTypeGateway;
import com.nidu.demo.dict.model.DictType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 字典类型单查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class DictTypeSingleQryExe {

    private final DictTypeGateway gateway;

    private final DictTypeConvertor convertor;

    public SingleResponse<DictTypeCO> execute(Long id) {
        // 1. 查询数据
        DictType dictType = gateway.getById(id);
        if(dictType == null){
            throw new BizException(ErrorCodeConstants.DICT_TYPE_NOT_FOUND.getCode(), ErrorCodeConstants.DICT_TYPE_NOT_FOUND.getMessage());
        }
        // 2. 转换为DTO返回
        DictTypeCO dictTypeCO = convertor.toClientObject(dictType);

        return SingleResponse.of(dictTypeCO);
    }

}